package cn.tedu.eb_film.redis.impl;


import cn.tedu.eb_film.pojo.vo.MovieListVO;
import cn.tedu.eb_film.pojo.vo.MovieStandardVO;
import cn.tedu.eb_film.redis.IMovieRedisRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Repository
public class MovieRedisRepositoryImpl implements IMovieRedisRepository {

   
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public void save(MovieStandardVO movieStandardVO) {
        //将电影详情数据存入到Redis中
        String key = ITEM_KEY_PREFIX +movieStandardVO.getId();
        log.debug("即将向Redis中写入数据,key={},value={}",key,movieStandardVO);
        redisTemplate.opsForValue().set(key,movieStandardVO);
        //将以上的key也存入Redis的Set中
        redisTemplate.opsForSet().add(ALL_ITEM_KEYS_KEY,key);
    }

    @Override
    public MovieStandardVO get(Long id) {
        String key = ITEM_KEY_PREFIX + id;
        log.debug("即将向Redis中取出数据,key={}",key);
        Serializable serializable =  redisTemplate.opsForValue().get(key);
        MovieStandardVO movieStandardVO = null;
        if (serializable!=null){
            movieStandardVO = (MovieStandardVO) serializable;
        }
        return movieStandardVO;
    }

    @Override
    public Boolean delete(Long id) {
        String key = ITEM_KEY_PREFIX + id;
        log.debug("即将删除Redis中的数据key为:{}",key);
        return redisTemplate.delete(key);
    }

    @Override
    public Boolean deleteList() {
        String key = LIST_KEY;
        log.debug("即将删除Redis中的列表数据key为:{}",key);
        return redisTemplate.delete(key);
    }
    public long deleteAllItem(){
        //Set<String> keys = redisTemplate.keys("movie:item:*");
        Set<Serializable> members = redisTemplate.opsForSet().members(ALL_ITEM_KEYS_KEY);
        Set<String> keys = new HashSet<>();
        for (Serializable member:members){
            keys.add(member.toString());
        }
        keys.add(ALL_ITEM_KEYS_KEY);
        return redisTemplate.delete(keys);
    }

    @Override
    public void save(List<MovieListVO> movieList) {
        String key = LIST_KEY;
        ListOperations<String, Serializable> opsForList = redisTemplate.opsForList();
        for (MovieListVO movieListItemVO : movieList) {
            log.debug("即将向Redis中写入列表项数据，Key={}，列表项={}", key, movieListItemVO);
            opsForList.rightPush(key, movieListItemVO);
        }
    }

    @Override
    public List<MovieListVO> list() {
        return list(0L,-1L);
    }

    @Override
    public List<MovieListVO> list(Long start, Long end) {
        String key = LIST_KEY;
        List<MovieListVO> list = new ArrayList<>();
        List<Serializable> serializables = redisTemplate.opsForList().range(key,start,end);
        for (Serializable serializable : serializables) {
            list.add((MovieListVO) serializable);
        }
        return list;
    }

    @Override
    public Long countRelease() {
        String key = LIST_KEY;
        return redisTemplate.opsForList().size(LIST_KEY);
    }

    @Override
    public List<MovieListVO> listPage(Long start, Long end) {
        return list(start,end);
    }
}
